{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "46dc2beb-8580-4eca-9a45-e0be76831800",
   "metadata": {},
   "source": [
    "# Exercise: Feature Engineering for Trading Models \n",
    "\n",
    "In this exercise, you'll get practice engineering features for trading models. You can use built-in Pandas methods to do this feature engineering. In the demo, we'll show you how to use a more specialized library called `ta` to do technical analysis feature engineering. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "95c38983-59ee-4cc1-8014-8befef7b75cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import yfinance as yf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80557948-987d-4392-bfe6-37c126a8d419",
   "metadata": {},
   "source": [
    "**Pull data for one stock ticker from YahooFinance**\n",
    "\n",
    "Use the YF API to pull daily price data for at least 2 years for any stock ticker you'd like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "16691348-683e-4eb7-baac-ef1e62740e31",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_ticker = 'AAPL'\n",
    "start_date = '2020-01-01' # use format YYYY-MM-DD\n",
    "end_date = '2024-01-02'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "47db4008-40f1-47fa-a91e-b5788b1d98c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[*********************100%***********************]  1 of 1 completed\n"
     ]
    }
   ],
   "source": [
    "data = yf.download( stock_ticker, start= start_date, end = end_date ) # replace ... inside this function with the correct parameters in order to get your data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "590c0326-987e-4ac7-8d7e-2bc2372c3aee",
   "metadata": {},
   "source": [
    "**Calculate the 10-day momentum for the above ticker's closing price**\n",
    "\n",
    "Recall that the 10-day momentum is the rate of change of a price over a 10-day period. It's used in technical analysis to see in which direction and with what magnitude an asset's price is moving. \n",
    "\n",
    "To calculate the rate of change, recall you can use the Pandas method `pct_change()`. To get a 10-day rate of change speficially, you'll have to pass in some parameter to the `pct_change()` method. Reading the documentation for that method may help: \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b9338e55-1764-4411-bbea-d3f52a6c5f12",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['10_day_momentum'] = data['Close'].pct_change(periods=10) # replace ... to get the answer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf0e0c80-b665-4c86-84cf-b909f86dd90d",
   "metadata": {},
   "source": [
    "**Calculate a 12-day and 26-day exponential moving average**\n",
    "\n",
    "Using the closing price for your stock, use Pandas to calculate a 12-day and 26-day EMA (exponential moving average). Look into the Pandas method `ewm()`, which was used in the demo. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "80e8373a-b82c-4b35-8692-618d294761d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['EMA_12'] = data['Close'].ewm(span=12).mean()\n",
    "data['EMA_26'] = data['Close'].ewm(span=26).mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a132f0b0-e615-4342-a004-4c88c83e762a",
   "metadata": {},
   "source": [
    "**Manually calculate the MACD (moving average convergence divergence)**\n",
    "\n",
    "Recall that the MACD is calculated as the 12-day exponential moving average minus the 26-day. Use the above step to calculate the MACD. You'll have to create your own column for this step. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f06753e5-4ad4-421c-aa4e-c6208dadc70a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculate the MACD and save it to a new column in your dataframe \n",
    "data['MACD'] = data['EMA_12'] - data['EMA_26']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d48acfe9-451e-4514-94bc-70744ecf8b46",
   "metadata": {},
   "source": [
    "**Manually calculate the MACD Signal**\n",
    "\n",
    "Recall that the MACD signal (discussed in the feature engineering demo) is calculated as the 9-period exponential moving average of the MACD (calculated in the prior step). Can you manually use Pandas methods to calculate the MACD signal? Create a new column for it in your dataframe. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "aa8f61e8-48c3-4945-b3cf-fcb187cecd44",
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculate the MACD signal using the above MACD using only Pandas methods (don't use the ta library shown in the demo) \n",
    "\n",
    "data['MACD_signal'] = data['MACD'].ewm(span=9).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "636c02fc-1652-4592-8e6c-20f8005af95e",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = data.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bbc51474-5a18-4841-990e-953d51a000ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>10_day_momentum</th>\n",
       "      <th>EMA_12</th>\n",
       "      <th>EMA_26</th>\n",
       "      <th>MACD</th>\n",
       "      <th>MACD_signal</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-16</th>\n",
       "      <td>78.397499</td>\n",
       "      <td>78.925003</td>\n",
       "      <td>78.022499</td>\n",
       "      <td>78.809998</td>\n",
       "      <td>76.488976</td>\n",
       "      <td>108829200</td>\n",
       "      <td>0.049575</td>\n",
       "      <td>77.459831</td>\n",
       "      <td>77.077168</td>\n",
       "      <td>0.382662</td>\n",
       "      <td>0.254621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-17</th>\n",
       "      <td>79.067497</td>\n",
       "      <td>79.684998</td>\n",
       "      <td>78.750000</td>\n",
       "      <td>79.682503</td>\n",
       "      <td>77.335770</td>\n",
       "      <td>137816400</td>\n",
       "      <td>0.071614</td>\n",
       "      <td>77.855014</td>\n",
       "      <td>77.397275</td>\n",
       "      <td>0.457740</td>\n",
       "      <td>0.298242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-21</th>\n",
       "      <td>79.297501</td>\n",
       "      <td>79.754997</td>\n",
       "      <td>79.000000</td>\n",
       "      <td>79.142502</td>\n",
       "      <td>76.811691</td>\n",
       "      <td>110843200</td>\n",
       "      <td>0.055937</td>\n",
       "      <td>78.078571</td>\n",
       "      <td>77.601728</td>\n",
       "      <td>0.476843</td>\n",
       "      <td>0.336041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-22</th>\n",
       "      <td>79.644997</td>\n",
       "      <td>79.997498</td>\n",
       "      <td>79.327499</td>\n",
       "      <td>79.425003</td>\n",
       "      <td>77.085876</td>\n",
       "      <td>101832400</td>\n",
       "      <td>0.064714</td>\n",
       "      <td>78.307825</td>\n",
       "      <td>77.806503</td>\n",
       "      <td>0.501322</td>\n",
       "      <td>0.370618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-23</th>\n",
       "      <td>79.480003</td>\n",
       "      <td>79.889999</td>\n",
       "      <td>78.912498</td>\n",
       "      <td>79.807503</td>\n",
       "      <td>77.457108</td>\n",
       "      <td>104472000</td>\n",
       "      <td>0.052904</td>\n",
       "      <td>78.559047</td>\n",
       "      <td>78.022962</td>\n",
       "      <td>0.536085</td>\n",
       "      <td>0.404918</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 Open       High        Low      Close  Adj Close     Volume  \\\n",
       "Date                                                                           \n",
       "2020-01-16  78.397499  78.925003  78.022499  78.809998  76.488976  108829200   \n",
       "2020-01-17  79.067497  79.684998  78.750000  79.682503  77.335770  137816400   \n",
       "2020-01-21  79.297501  79.754997  79.000000  79.142502  76.811691  110843200   \n",
       "2020-01-22  79.644997  79.997498  79.327499  79.425003  77.085876  101832400   \n",
       "2020-01-23  79.480003  79.889999  78.912498  79.807503  77.457108  104472000   \n",
       "\n",
       "            10_day_momentum     EMA_12     EMA_26      MACD  MACD_signal  \n",
       "Date                                                                      \n",
       "2020-01-16         0.049575  77.459831  77.077168  0.382662     0.254621  \n",
       "2020-01-17         0.071614  77.855014  77.397275  0.457740     0.298242  \n",
       "2020-01-21         0.055937  78.078571  77.601728  0.476843     0.336041  \n",
       "2020-01-22         0.064714  78.307825  77.806503  0.501322     0.370618  \n",
       "2020-01-23         0.052904  78.559047  78.022962  0.536085     0.404918  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4b93ebc-d84d-4fd0-b0e7-f3fd14caf892",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
